import store from "@/store";
import { RESystemReady, setupEngine } from "./Engine";

let listenerMap = [
  //黑洞引擎系统初始化完成回调
  {
    event: "RESystemReady",
    func: () => {
      store.dispatch("setStoreState", {
        BLACKhOLE_RE_SYSTEM_READY: true,
      });
      RESystemReady();
    },
  },
  //系统引擎创建完成回调
  {
    event: "RESystemEngineCreated",
    func: (e) => {
      console.info("【黑洞】==>>> (RESystemEngineCreated)系统引擎创建完成回调");
      let isSuccess = e.detail.succeed;
      if (isSuccess) {
        setTimeout((_) => {
          store.dispatch("setStoreState", {
            BLACKhOLE_RE_SYSTEM_CREATED: true,
          });
        }, 100);
      } else {
        store.dispatch("setStoreState", {
          BLACKhOLE_RE_SYSTEM_CREATED: false,
        });
        console.warn("【黑洞】==>>> (RESystemEngineCreated)系统引擎创建失败");
      }
    },
  },
];

//添加事件监听
export function addEventListener() {
  listenerMap.forEach((v) => {
    let { event, func } = v;
    document.addEventListener(event, func);
  });
}

//移除事件监听
export function removeEventListener() {
  listenerMap.forEach((v) => {
    let { event, func } = v;
    document.removeEventListener(event, func);
  });
  store.dispatch("setStoreState", {
    BLACKhOLE_RE_SYSTEM_CREATED: false,
  });
}
